
package com.wyzz.hr.action;

import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.TreeNode;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;

public class EmpTreeInit extends BaseAction {

	public EmpTreeInit() {
		nodeList = null;
	}

	public String execute() throws Exception {
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		nodeList = posBo.generateEmpConnTreeNodes();
		for (int i = 0; i < nodeList.size(); i++) {
			if ("0".equals(((TreeNode) nodeList.get(i)).getNodeParentId())) {
				this.rootId = ((TreeNode) nodeList.get(i)).getNodeParentId();
				break;
			}
		}
		this.id = URLDecoder.decode(getRequest().getParameter("id"), "UTF-8");
		if ((this.id == null) || (this.id.equals(""))) {
			this.id = getCurrentEmpNo();
		}
		searchEmpId();
		return "success";
	}

	private void searchEmpId() {
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		BaseCrit.addEmpDC(dc, "", this.id);
		List list = empBo.findByCriteria(dc);
		Employee employee = new Employee();
		if ((list == null) || (list.isEmpty()))
			employee = empBo.loadEmp(this.id, null);
		else
			employee = (Employee) list.get(0);
		this.id = employee.getId();
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Position position = posBo.getPosByEmpNo(this.id, new String[0]);
		if (position == null)
			return;
		this.posId = position.getId();
	}

	public List getNodeList() {
		return nodeList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPosId() {
		return posId;
	}

	public void setPosId(String posId) {
		this.posId = posId;
	}

	public void setNodeList(List nodeList) {
		this.nodeList = nodeList;
	}

	public String getRootId() {
		return rootId;
	}

	public void setRootId(String rootId) {
		this.rootId = rootId;
	}

	private static final long serialVersionUID = 1L;
	private List<TreeNode> nodeList;
	private String rootId;
	private String id;
	private String posId;
}
